package com.scores.service.impl;

import com.scores.mapper.ScoreMapper;
import com.scores.mapper.StudentMapper;
import com.scores.pojo.Score;
import com.scores.pojo.Student;
import com.scores.pojo.ThreeS;
import com.scores.service.StudentService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentMapper studentMapper;
    @Autowired
    ScoreMapper scoreMapper;

    @Override
    public List<Student> getAllStudents() {
        return studentMapper.getAllStudent(new Student());
    }

    @Override
    public List<Student> getAllStudentsByPage(Student student, int page, int size) {
        if(student.getStudentName()!=null && !student.getStudentName().trim().isEmpty()) {
            student.setStudentName("%"+student.getStudentName().trim()+"%");
        }
        return studentMapper.getAllStudentByPage(student, page, size);
    }

    @Override
    public int getAllStudentsCount(Student student) {
        if(student.getStudentName()!=null && !student.getStudentName().trim().isEmpty()) {
            student.setStudentName("%"+student.getStudentName().trim()+"%");
        }
        return studentMapper.getAllStudentCount(student);
    }

    @Override
    public int addStudent(Student student) {
        Integer studentNo = student.getStudentNo();
        Student s = studentMapper.getStudentByNo(studentNo);
        if(s!=null){
            return 1;
        }
        List<Student> studentList = new ArrayList<>();
        studentList.add(student);
        int count = studentMapper.addStudent(studentList);
        if(count>0){
            return 0; // Success
        } else {
            return 2; // Failure
        }
    }

    @Override
    public int updateStudent(Student student) {
        int count = studentMapper.updateStudent(student);
        if(count>0){
            return 0; // Success
        } else {
            return 1; // Failure
        }
    }

    @Override
    public int deleteStudent(Student student) {

        List<ThreeS> threeS = scoreMapper.getScoreByStudentNo(student.getStudentNo(), null, null);
        System.out.println(threeS);
        if(threeS!=null && !threeS.isEmpty()){
            return 2;
        }
        int count1 = studentMapper.deleteStudentByNo(student);
        if(count1 > 0){
            return 0; // Number of deleted students
        } else {
            return 1; // Failure
        }
    }

    @Override
    public int updatePassword(Integer studentNo, String newPassword) {
        int count = studentMapper.updatePassword(studentNo, newPassword);
        if(count > 0){
            return 0; // Success
        } else {
            return 1; // Failure
        }
    }

    @Override
    public String getStudentPassword(Integer studentNo) {
        return studentMapper.getStudentByNo(studentNo).getStudentPassword();
    }
}
